from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from cart.cartser import CartSerializer
from cart.models import CartModel
from order.models import UserCouponModel
from order.orderser import UserCouponSerializer
from user.models import UserModel
from utils.authorization import MyAuthentication


# Create your views here.


class GetCartView(ListAPIView):
    authentication_classes = [MyAuthentication]
    queryset = CartModel.objects.all()
    serializer_class = CartSerializer

    def get_queryset(self):
        user = self.request.query_params.get('user')
        if user:
            return self.queryset.filter(user=user,select=True)

class CouponView(APIView):
    authentication_classes = [MyAuthentication]
    def get(self,request):
        user = request.query_params.get('user')
        userinfo = UserModel.objects.get(id = user)
        mycoupon = UserCouponModel.objects.filter(user=user)
        ser = UserCouponSerializer(instance=mycoupon,many=True)
        return Response({'code':200,'msg':'请求成功','data':{'credit':userinfo.credit,'tomoney':10,'coupon':ser.data}})


